package com.isoftstone.bank.controller;


import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.bank.entity.PAd;
import com.isoftstone.bank.service.IPAdService;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.util.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

/**
 * Created by xwj on 2017/11/28.
 */
@Controller
public class AdController extends BaseController {

        @Autowired
        private IPAdService service;

        @RequiresPermissions("bms-menu.003.001")
        @RequestMapping("/bank/ad")
        public ModelAndView toAdPage(Page page){
                ModelAndView mv = new ModelAndView("/bank/ad/ad_list");
                return mv;
        }

        @RequestMapping("/bank/ad/getAdlist")
        @ResponseBody
        public Object getAdlist(Page page) throws UnsupportedEncodingException {
                Tidings<List<PAd>> tidings = new Tidings<List<PAd>>();
                PageData pd = this.getPageData();
                String keywords = pd.getString("keywords");
                if (keywords != null) {
                        String keywords1 = URLDecoder.decode(keywords, "UTF-8");
                        pd.put("keywords", keywords1);
                }
                page.setPd(pd);
                List<PAd> list= service.getAdlistPage(page);
                tidings.setT(list).setPage(page);
                tidings.setStatus(Const.SUCCESS);
                return tidings;
        }

        @RequestMapping("/bank/ad/editQuery")
        @ResponseBody
        public Object editQuery(String adId){
                Tidings<PAd> tidings = new Tidings<>();
                if(Tools.notEmpty(adId)){
                        PAd bean = service.selectByPrimaryKey(Long.valueOf(adId));
                        tidings.setT(bean);
                }
                tidings.setStatus(Const.SUCCESS);
                return tidings;
        }
        @RequestMapping("/bank/ad/save")
        @ResponseBody
        public Object save(@ModelAttribute("pad") PAd pad){
                Tidings<String> tidings = new Tidings<String>();
                try {
                        if (pad.getAdId() == null) {
                                //pad.setStatus("1");
                                pad.setCreateDate(new Date());
                                pad.setCreator(this.getUser().getUserid());
                                service.insertSelective(pad);
                                tidings.setStatus(Const.SUCCESS);
                                tidings.setMsg("保存成功！");

                        } else {
                                pad.setUpdateDate(new Date());
                                pad.setUpdator(this.getUser().getUserid());
                                service.updateByPrimaryKeySelective(pad);
                                tidings.setStatus(Const.SUCCESS);
                                tidings.setMsg("修改成功！");
                        }
                } catch (Exception e) {
                        tidings.setStatus(Const.ERROR);
                        tidings.setMsg("保存失败！");
                        e.printStackTrace();
                }
                return tidings;
        }

        @RequestMapping("/bank/ad/del")
        @ResponseBody
        public Tidings<String> del(Long adId){
                Tidings<String> tidings = new Tidings<String>();
                try {
                        service.deleteByPrimaryKey(adId);
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("删除成功！");
                } catch (Exception e) {
                        tidings.setStatus(Const.ERROR);
                        tidings.setMsg("删除失败！");
                        e.printStackTrace();
                }
                return tidings;
        }

        @RequestMapping("/bank/ad/change")
        @ResponseBody
        public Tidings<String> change(Long adId,String status){
                Tidings<String> tidings = new Tidings<String>();
                try {
                        PAd pad = new PAd();
                        pad.setAdId(adId);
                        pad.setStatus(status);
                        service.updateByPrimaryKeySelective(pad);
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("操作成功！");
                } catch (Exception e) {
                        tidings.setStatus(Const.ERROR);
                        tidings.setMsg("操作失败！");
                        e.printStackTrace();
                }
                return tidings;
        }

        @RequestMapping("/bank/ad/upload")
        @ResponseBody
        public String upload(@RequestParam("file_upload") MultipartFile multipartFile){
                String adFilePath = null;
                try {
                        adFilePath = FileUpload.prodFileUploadByRandomFileName(multipartFile, PathUtil.getClasspath()+Const.FILEPATHIMG);
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return adFilePath;
        }

        @RequestMapping("/bank/ad/removeImg")
        @ResponseBody
        public Object removeImg(String fileName){
                Tidings<String> tiding = new Tidings<>();
                File file = new File(PathUtil.getClasspath()+Const.FILEPATHIMG+fileName);
                file.getAbsoluteFile().delete();
                tiding.setStatus(Const.SUCCESS);
                return tiding;
        }


}
